package com.edu.service;

import com.edu.bean.SubjectInfo;
import com.edu.bean.StudentScore;
import com.edu.bean.User;
import com.edu.dao.SubjectInfoDao;
import com.edu.dao.StudentScoreDao;
import com.edu.dao.UserDao;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class AdminSubjectService {
    private SubjectInfoDao subjectInfoDao = new SubjectInfoDao();
    private StudentScoreDao studentScoreDao = new StudentScoreDao();
    private UserDao userDao = new UserDao();

    /**
     * 添加学科信息
     */
    public boolean adminAddSubject(SubjectInfo subject) {
        // 检查时间段是否被占用
        if (subjectInfoDao.isTimeSlotOccupied(subject.getClassroom(),
                subject.getWeekDay(), subject.getClassTime(), null)) {
            return false;
        }
        return subjectInfoDao.add(subject);
    }

    /**
     * 更新学科信息
     */
    public boolean adminUpdateSubject(SubjectInfo subject) {
        // 检查时间段是否被占用（排除自身）
        if (subjectInfoDao.isTimeSlotOccupied(subject.getClassroom(),
                subject.getWeekDay(), subject.getClassTime(), subject.getSubjectId())) {
            return false;
        }
        return subjectInfoDao.update(subject);
    }

    /**
     * 删除学科信息
     */
    public boolean adminDeleteSubject(Integer subjectId) {
        return subjectInfoDao.delete(subjectId);
    }

    /**
     * 获取所有学科信息
     */
    public List<SubjectInfo> adminListSubjects() {
        return subjectInfoDao.findAll();
    }

    /**
     * 获取单个学科信息
     */
    public SubjectInfo adminGetSubject(Integer subjectId) {
        return subjectInfoDao.findById(subjectId);
    }

    /**
     * 添加学生成绩
     */
    public boolean adminAddScore(StudentScore score) {
        // 检查是否已有成绩
        if (studentScoreDao.hasScore(score.getStudentId(), score.getSubjectId())) {
            return false;
        }
        return studentScoreDao.add(score);
    }

    /**
     * 更新学生成绩
     */
    public boolean adminUpdateScore(StudentScore score) {
        return studentScoreDao.update(score);
    }

    /**
     * 删除学生成绩
     */
    public boolean adminDeleteScore(Integer scoreId) {
        return studentScoreDao.delete(scoreId);
    }

    /**
     * 获取所有学生成绩
     */
    public List<StudentScore> adminListScores() {
        return studentScoreDao.findAll();
    }

    /**
     * 根据学科ID获取学生成绩
     */
    public List<StudentScore> adminListScoresBySubject(Integer subjectId) {
        return studentScoreDao.findBySubjectId(subjectId);
    }

    /**
     * 获取单个学生成绩
     */
    public StudentScore adminGetScore(Integer scoreId) {
        return studentScoreDao.findById(scoreId);
    }

    /**
     * 获取所有教师列表
     */
    public List<User> adminListTeachers() {
        return userDao.findByRole(3); // 3=教师
    }

    /**
     * 获取所有学生列表
     */
    public List<User> adminListStudents() {
        return userDao.findByRole(2); // 2=学生
    }

    /**
     * 分页获取学科信息
     */
    public Map<String, Object> adminListSubjects(int page, int limit, String search) {
        Map<String, Object> result = new HashMap<>();

        int start = (page - 1) * limit;
        List<SubjectInfo> list = subjectInfoDao.findByPage(start, limit, search);
        int total = subjectInfoDao.findTotalCount(search);

        result.put("list", list);
        result.put("total", total);
        return result;
    }

    /**
     * 获取学科总记录数
     */


    /**
     * 分页获取成绩信息
     */
    public List<StudentScore> adminListScoresByPage(Integer subjectId, int currentPage, int pageSize) {
        int start = (currentPage - 1) * pageSize;
        return studentScoreDao.findByPage(subjectId, start, pageSize);
    }

    /**
     * 获取成绩总记录数
     */
    public int adminGetScoreTotalCount(Integer subjectId) {
        return studentScoreDao.findTotalCount(subjectId);
    }
}